package com.lin.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.lin.entity.ProjectDetail;
import com.lin.entity.ProjectOutside;
import com.lin.entity.ProjectStatus;
import com.lin.entity.Vote;
import com.lin.mapper.ProjectDetailMapper;
import com.lin.mapper.ProjectOutsideMapper;
import com.lin.mapper.ProjectStatusMapper;
import com.lin.mapper.VoteMapper;
import com.lin.security.util.SecurityUtil;
import com.lin.service.ProjectStatusService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;

/**
 * <p>
 * 服务实现类
 * </p>
 *
 * @author 林伟鸿
 * @since 2022-04-18
 */
@Service
public class ProjectStatusServiceImpl extends ServiceImpl<ProjectStatusMapper, ProjectStatus> implements ProjectStatusService {

    @Resource
    ProjectOutsideMapper projectOutsideMapper;
    @Resource
    ProjectDetailMapper projectDetailMapper;
    @Resource
    VoteMapper voteMapper;

    @Override
    public void startVote(ProjectStatus projectStatus) {
        if (projectOutsideMapper.selectById(projectStatus.getProjectId()) == null) {
            throw new RuntimeException("发起投票的项目不存在！");
        }
        ProjectOutside projectOutside = projectOutsideMapper.selectById(projectStatus.getProjectId());
        int currentUserUid = SecurityUtil.getCurrentUserUid();
        if (projectOutside.getMasterId().equals(currentUserUid)) {
            if (this.getById(projectStatus.getProjectId()) != null) {
                throw new RuntimeException("请不要重复发起投票");
            }
            projectStatus.setIsOnVote(1);
            this.save(projectStatus);
        }else {
            throw new RuntimeException("您不是项目队长，无法进行操作");
        }


    }
}
